#include "RIVIAEntityManager.h"
#include "RIVIAImage.h"

RIVIAEntityManager::RIVIAEntityManager():
    m_nCurImageID(0),
    m_nMaxImageID(1),
    m_pImagesHash(nullptr)
{
    m_pImagesHash = new QHash<int, RIVIAImage*>;
}

RIVIAEntityManager::~RIVIAEntityManager()
{
    QHash<int, RIVIAImage*>::iterator Iter;
    for(Iter = m_pImagesHash->begin(); Iter != m_pImagesHash->end(); ++Iter)
    {
        delete Iter.value();
    }
    delete m_pImagesHash;
    m_pImagesHash = nullptr;
}

RIVIAImage *RIVIAEntityManager::newImage(const cv::Mat &Mat)
{
    int nID = m_nMaxImageID++;
    RIVIAImage* pImage = new RIVIAImage(Mat);
    m_pImagesHash->insert(nID, pImage);
    setCurImageID(nID);
    return pImage;
}

RIVIAImage *RIVIAEntityManager::getCurImage()
{
    RIVIAImage* pImage = nullptr;
    QHash<int, RIVIAImage*>::iterator Iter = m_pImagesHash->find(m_nCurImageID);
    if(Iter != m_pImagesHash->end())
        pImage = Iter.value();
    return pImage;
}

void RIVIAEntityManager::deleteImageByID(int nImageID)
{
    QHash<int, RIVIAImage*>::iterator Iter = m_pImagesHash->find(nImageID);
    if(Iter != m_pImagesHash->end())
    {
        RIVIAImage* pImage = Iter.value();
        delete pImage;
        m_pImagesHash->remove(nImageID);
    }
}

void RIVIAEntityManager::setCurImageID(int nImageID)
{
    m_nCurImageID = nImageID;
}
